package cn.springboot.erp.service.impl;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.RolePers;
import cn.springboot.erp.mapper.RolePersMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.Permission;
import cn.springboot.erp.service.PermissionService;
import cn.springboot.erp.mapper.PermissionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author asus
* @description 针对表【permission】的数据库操作Service实现
* @createDate 2024-05-04 21:51:02
*/
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{

    @Resource
    private RolePersMapper rolePersMapper;

    @Override
    public ResponseResult listPage(String title) {

        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(StringUtils.isNotBlank(title),Permission::getTitle,title);
        List<Permission> permissionList = baseMapper.selectList(lambda);

        return ResponseResult.Success().data("permissionList",permissionList);
    }

    @Override
    public ResponseResult deletePermission(Integer id) {
        // id表示权限id
        // 根据权限id查询 是否有以权限id作为父组件的pid
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getPid,id);
        List<Permission> list = baseMapper.selectList(lambda);

        // 如果查询结果list不为空，则表示有当前目录下有子目录
        if(ObjectUtils.isNotNull(list)){
            return ResponseResult.Fail().msg("当前权限包含子节点，不能删除");
        }
        // 否则查询结果list为空，则表示当前目录下没有子目录，可以删除
        baseMapper.deleteById(id);

        // 删除关联表中的数据 sys_role_permission
        LambdaQueryWrapper<RolePers> lambda1 = new QueryWrapper<RolePers>().lambda();
        lambda1.eq(RolePers::getPerId,id);
        rolePersMapper.delete(lambda1);
        return ResponseResult.Success().msg("删除成功");
    }
}




